function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) { return pair[1]; }
    }
    return (false);
}



//检查用户是否已经登录的组件
var app2 = new Vue({
    el: "#app2",
    data: {
        user_id: -1,
        user_name: ''
    },
    methods: {

    },
    mounted: function() {
        instance({
            method: 'get',
            url: 'api/user/getIdBySession',

        }).then(res => {
            this.user_id = res.data.data.user_id
            this.user_name = res.data.data.user_name

        }).catch(err => {
            console.log(err);
        })
    }
})




var app = new Vue({
    el: "#app",
    data: {
        goods: {

        },
        num: 1
    },
    methods: {
        reduce: function() { //数量减
            if (this.num > 1)
                this.num--;
            else
                window.alert("不能再少了!")
        },
        add: function() { //数量加
            if (this.num < this.goods.goods_number)
                this.num++;
            else
                window.alert("不能再多了!")
        },

        buy: function() { //购买
            if (app2.user_id == -1) {
                notLogin()

            } else {

                var param = new FormData()
                param.append("user_id", app2.user_id)
                param.append("goods_id", getQueryVariable("goods_id"))
                param.append("order_number", app.num)
                instance({
                    method: "post",
                    url: 'api/pay/confirm',
                    data: param
                }).then(res => {
                    show("结算中...")
                    const div = document.createElement('divform');
                    div.innerHTML = res.data;
                    document.body.appendChild(div);
                    document.forms[0].acceptCharset = 'utf-8'; //保持与支付宝默认编码格式一致，如果不一致将会出现：调试错误，请回到请求来源地，重新发起请求，错误代码 invalid-signature 错误原因: 验签出错，建议检查签名字符串或签名私钥与应用公钥是否匹配
                    document.forms[0].submit();
                }).catch(err => {

                })

            }

        },
        addToCart: function() { //添加到购物车
            console.log("addToCart")
            if (app2.user_id == -1) { //判断用户是否登录
                notLogin()

            } else { //如果登录插入数据
                instance({
                    method: 'get',
                    url: 'api/shopping/insert?goods_id=' + getQueryVariable("goods_id") + '&goods_number=' + app.num,

                }).then(res => {
                    layui.use('layer', function() { //独立版的layer无需执行这一句
                        layer = layui.layer; //独立版的layer无需执行这一句
                        layer.msg('添加购物车成功!', {
                            time: 20000, //20s后自动关闭
                            btn: ['查看购物车', '确认'],
                            btn1: function() {
                                window.location = "cart.html"
                            }
                        });

                    });

                }).catch(err => {
                    console.log(err);
                })

            }
        }
    },
    mounted: function() {

        axios.get("http://47.119.134.71:8080/api/goods/getById?goods_id=" + getQueryVariable("goods_id"))
            .then(res => {
                this.goods = res.data.data

            })
            .catch(err => {
                console.error(err);
            })
    }
})


//判断没有登录时 执行的函数
function notLogin() {
    layui.use('layer', function() { //独立版的layer无需执行这一句
        layer = layui.layer; //独立版的layer无需执行这一句
        layer.msg('您未登录!请先登录', {
            time: 20000, //20s后自动关闭
            btn: ['立即登录', '看看再说'],
            btn1: function() {
                window.location = "login.html"
            }
        });

    });
}


//layui 购买 &  添加购物车逻辑
layui.use('layer', function() { //独立版的layer无需执行这一句
    var $ = layui.jquery,
        layer = layui.layer; //独立版的layer无需执行这一句

    //触发事件
    var active = {

        add: function() {
            console.log("hello")
            if (app2.user_id = -1) {
                //配置一个透明的询问框
                layer.msg('您未登录!请先登录', {
                    time: 20000, //20s后自动关闭
                    btn: ['立即登录', '看看再说'],
                    btn1: function() {
                        console.log("hello")
                        window.location = "login.html"
                    }
                });
            }

        },


    };

});